﻿using DAL_13_0;
using DAL_13_0.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.DataAccess;

namespace RFID_Interface.DTO
{
    public class RecieveBill
    {
        const int TRANTYPE = 2;

        public string SerialNo { get; set; }
        public DateTime BillDate { get; set; }
        public string StockNumber { get; set; }
        public string DepNumber { get; set; }
        public int UserID { get; set; }

        public List<RecieveBillEntry> Entry { get; set; }

        public RecieveBillResponse Save()
        {
            using (var db = DB.Open())
            {
                //todo:数据校验

                db.BeginTrans();

                int dcstockid;
                dcstockid = DAL_13_0.Entities.Item.Get(db, StockNumber, 5).ID;

                var bill = new ICStockBill();
                bill.FInterID = IdentityID.GetNewICBillID(db, ICStockBill.GetSQLTableName());
                bill.FTranType = TRANTYPE;
                bill.FBillerID = UserID;
                bill.FCheckerID = null;
                bill.FDate = BillDate;
                bill.FDeptID = DAL_13_0.Entities.Item.GetID(db, DepNumber, 2);

                var billentry = new List<ICStockBillEntry>();
                var barcodeEntry = new List<ICStockBillEntryBarcode>();

                foreach (var en in Entry)
                {
                    AddEntry(db, billentry, barcodeEntry, en);
                }

                foreach (var en in billentry)
                {
                    en.FInterID = bill.FInterID;
                    en.FDCStockID = dcstockid;
                    db.Insert(en);
                }

                foreach (var be in barcodeEntry)
                {
                    be.FInterID = bill.FInterID;
                    db.Insert(be);
                }

                bill.FBillNo = new BillNo(TRANTYPE.ToString()).GetNewNumber(db, true);
                db.Insert(bill);

                db.Commit();

                return new RecieveBillResponse { Result = 'S', BillNo = bill.FBillNo, SerialNo = SerialNo };
            }
        }
        private void AddEntry(DbSession db, List<ICStockBillEntry> billentry, List<ICStockBillEntryBarcode> barcodeEntry, RecieveBillEntry entryDTO)
        {
            var item = DAL_13_0.Entities.ICItem.Get(db, entryDTO.ICItemNumber, 4);
            if (item == null) throw new Exception("物料编码错误");

            var pdate = DateTime.Parse(entryDTO.PDate);

            var entry = billentry.FirstOrDefault(e => e.FItemID == item.FItemID
                && e.PLine == entryDTO.PLine
                && e.WorkClass == entryDTO.WorkClass
                && e.PDate == pdate);

            if (entry == null)
            {
                entry = new ICStockBillEntry { FEntryID = billentry.Count + 1, FItemID = item.FItemID, FUnitID = item.FUnitID };

                //生产线
                entry.PLine = entryDTO.PLine;

                //班组
                entry.WorkClass = entryDTO.WorkClass;

                //班组
                entry.PDate = pdate;

                billentry.Add(entry);
            }

            var packageUnit = db.QuerySingle<DAL_13_0.Entities.MeasureUnit>(DAL_13_0.Entities.MeasureUnit.pID == item.PackageUnitID);
            entry.FQty += packageUnit.Coefficient;

            barcodeEntry.Add(new ICStockBillEntryBarcode { FEntryID = entry.FEntryID, FTrantype = TRANTYPE, FBarcode = entryDTO.Barcode });
        }
    }
    public class RecieveBillEntry
    {
        public string Barcode { get; set; }
        public string ICItemNumber { get; set; }
        public decimal Qty { get; set; }
        public string Unit { get; set; }
        public string PLine { get; set; }
        public string PDate { get; set; }
        public string WorkClass { get; set; }

    }

    public class RecieveBillResponse
    {
        public char Result { get; set; }
        public string BillNo { get; set; }
        public string SerialNo { set; get; }
        public string ErrMsg { get; set; }
    }
}